﻿using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class UnityDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
	[SerializeField]
	private List<TKey> _keys = new List<TKey>();
	[SerializeField]
	private List<TValue> _values = new List<TValue>();
#if UNITY_EDITOR
	//[SerializeField]
	List<string> _tips = new List<string>();
#endif

	public void OnBeforeSerialize()
	{
		_keys.Clear();
		_values.Clear();
#if UNITY_EDITOR
		_tips.Clear();
		bool isValueType = typeof(TValue).IsValueType;
#endif
		_keys.Capacity = this.Count;
		_values.Capacity = this.Count;
#if UNITY_EDITOR
		_tips.Capacity = this.Count;
#endif
		foreach (var kvp in this)
		{
			_keys.Add(kvp.Key);
			_values.Add(kvp.Value);
#if UNITY_EDITOR
			_tips.Add($"{kvp.Key}=>{kvp.Value}");
#endif
		}
	}

	public void OnAfterDeserialize()
	{
		this.Clear();
#if UNITY_EDITOR
		_tips.Clear();
#endif
		int count = Mathf.Min(_keys.Count, _values.Count);
		for (int i = 0; i < count; ++i)
		{
			this.Add(_keys[i], _values[i]);
#if UNITY_EDITOR
			_tips.Add($"{_keys[i]}=>{_values[i]}");
#endif
		}
	}
}